package com.esm.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;

import com.esm.service.FuncService;
import com.esm.serviceimpl.FuncServiceImpl;
import com.esm.utils.PagingUtil;
import com.esm.utils.PrintWriterUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
/**
 * 王文成
 * @author Administrator
 *
 */
public class SearchFuncController extends HttpServlet{
	FuncService  fs=new FuncServiceImpl();
	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	
	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		String reqPage=req.getParameter("page");
		String reqPageSize=req.getParameter("pagesize");
		reqPage=StringUtils.isNotEmpty(reqPage)?reqPage:"1";
		reqPageSize=StringUtils.isNotEmpty(reqPageSize)?reqPageSize:"10";
		PrintWriter pw=resp.getWriter();
		String rname=req.getParameter("rname");
		
		
		int page=Integer.parseInt(reqPage);
		int pagesize=Integer.parseInt(reqPageSize);
		JSONArray farray=fs.getRoleAuthorityByRname(rname, page, pagesize);
		int scount=fs.getFuncScount();
		JSONArray pagingfunc=fs.getPagingFunc(page, pagesize);
		int totalpage=(scount/pagesize==0)?(scount/pagesize):(scount/pagesize+1);
		String pageHtml=PagingUtil.getPageList(scount,page,pagesize,"searchFunc",req);
		
		JSONObject json = new JSONObject();
		json.put("funcarray",farray);
		json.put("pagingfunc",pagingfunc);
		json.put("page",page);
		json.put("pagesize",pagesize);
		json.put("scount",scount);
		json.put("totalpage",totalpage);
		json.put("pageHtml",pageHtml);
    	PrintWriterUtil.flushJoAndClosePrintWriter(pw,json);
    	pw.write(json.toString());
	}

}
